#include "timer_action.h"
#include "timer_task.h"

extern timer_action_item_s _timer_action_start;
extern timer_action_item_s _timer_action_end;

timer_action_item_t timer_action_find(char *name)
{
    if (!name)
        return NULL;
    timer_action_item_t start = (timer_action_item_t)&_timer_action_start;
    timer_action_item_t end = (timer_action_item_t)&_timer_action_end;
    for (; start < end; start++)
    {
        if (strlen(name) == strlen(start->name) && strcmp(start->name, name) == 0)
            return start;
    }
    printf("Not find timer action: %s\n", name);
    return NULL;
}
uint8_t timer_action_call(char *name, uint8_t param, uint8_t handle)
{
    timer_action_item_t item = timer_action_find(name);
    if (!item)
        return 0;
    timer_action_item_s task = *item;
    printf("action:%p\r\n", task.cb);
    return task.cb(handle, param);
}

#define timer_json_put_str(key, value) len += snprintf(out + len, size - len, "\"" #key "\":\"%s\",", value)
#define timer_json_put_number(key, value) len += snprintf(out + len, size - len, "\"" #key "\":%d,", value)
#define timer_json_put_array(name) len += snprintf(out + len, size - len, "\"" #name "\":[")
#define timer_json_put_array_end()                    \
    do                                                \
    {                                                 \
        len--;                                        \
        len += snprintf(out + len, size - len, "],"); \
    } while (0)
uint16_t timer_action_item_to_json(timer_action_item_t item, char *out, uint16_t size)
{
    if (!item || !out || size < 128)
        return 0;
    /*
        {
            name:"dvc",
            handle:[
                {name:"chg", desc:"on/off charge", i18n:"充电状态", desc_i18n:"打开(1)/关闭(0) 充电"},
                .....
            ],
            params:[
                {name:"close", i18n:"关闭", value:0, handle:255},
                {name:"open", i18n:"打开", value:1, handle:255},
            ]
        }
    */
    uint16_t len = 0;
    len += snprintf(out + len, size - len, "{");

    timer_json_put_str(name, item->name);

    timer_json_put_array(handle);
    for (uint8_t i = 0; i < item->handles_len; i++)
    {
        timer_action_handle_item_t *handle = &item->handles[i];
        len += snprintf(out + len, size - len, "{");
        timer_json_put_str(name, handle->name);
        timer_json_put_str(desc, handle->desc);
        timer_json_put_str(i18n, handle->i18n);
        timer_json_put_str(desc_i18n, handle->desc_i18n);
        len--;
        len += snprintf(out + len, size - len, "},");
    }
    timer_json_put_array_end();

    timer_json_put_array(params);
    for (uint8_t i = 0; i < item->params_len; i++)
    {
        timer_action_param_item_t *param = &item->params[i];
        len += snprintf(out + len, size - len, "{");
        timer_json_put_str(name, param->name);
        timer_json_put_str(i18n, param->i18n);
        timer_json_put_number(value, param->value);
        timer_json_put_number(handle, param->handles);
        len--;
        len += snprintf(out + len, size - len, "},");
    }
    timer_json_put_array_end();

    len--;
    len += snprintf(out + len, size - len, "},");
    return len;
}

uint16_t timer_actions_to_json(char *out, uint16_t size)
{
    uint16_t len = 0;
    len += snprintf(out + len, size - len, "[");
    uint16_t last_len = len;

    timer_action_item_t start = (timer_action_item_t)&_timer_action_start;
    timer_action_item_t end = (timer_action_item_t)&_timer_action_end;
    for (; start < end; start++)
    {
        timer_action_item_s task = *start;
        len += timer_action_item_to_json(&task, out + len, size - len);
    }

    if (last_len != len)
        len--;
    len += snprintf(out + len, size - len, "],");
    return len;
}